<?php

namespace App\Http\Resources\CruiseCheck;

use App\Models\Cruise\CruiseCheckSelfDetailModel;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;

class SupplierTaskGroupPreviewResource extends JsonResource
{
    public static Collection $rmsUserCollection;

    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        $todayNotProduce = $this->when(
            $this->cruiseCheckSelfDetailRel,
            $this->cruiseCheckSelfDetailRel->where("date", date("Ymd", time()))
                                           ->where("status", CruiseCheckSelfDetailModel::STATUS_NOT_PRODUCE)
                                           ->count(),
            0
        );

        $totalCount = $this->when(
                $this->cruiseCheckSelfTaskCompanyMappingsRel,
                $this->cruiseCheckSelfTaskCompanyMappingsRel->count(),
                0
            ) - $todayNotProduce;


        $finishedCount = $this->when(
            $this->cruiseCheckSelfDetailRel,
            $this->cruiseCheckSelfDetailRel->where("date", date("Ymd", time()))
                                           ->where("status", CruiseCheckSelfDetailModel::STATUS_FINISH)
                                           ->count(),
            0
        );
        return [
            "id"                => $this->id,
            "fsCompanyName"     => $this->fsCompanyRel->name,
            "todayNotProduce"   => $todayNotProduce,
            "totalCount"        => $totalCount,
            "finishedCount"     => $finishedCount,
            "stayCount"         => $totalCount - $finishedCount,
            "kfManageInfo"      => [
                "id"     => $this->kfManagerId,
                "name"   => self::$rmsUserCollection->offsetExists($this->kfManagerId) ? self::$rmsUserCollection->offsetGet($this->kfManagerId)["username"] : "-",
                "mobile" => self::$rmsUserCollection->offsetExists($this->kfManagerId) ? self::$rmsUserCollection->offsetGet($this->kfManagerId)["mobile"] : "-",
            ],
            "companyManageInfo" => [
                "id"     => $this->companyManagerId,
                "name"   => self::$rmsUserCollection->offsetExists($this->companyManagerId) ? self::$rmsUserCollection->offsetGet($this->companyManagerId)["username"] : "-",
                "mobile" => self::$rmsUserCollection->offsetExists($this->companyManagerId) ? self::$rmsUserCollection->offsetGet($this->companyManagerId)["mobile"] : "-",
            ],
        ];
    }
}
